package com.city.parking;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.city.parking.map.OfflineMapSettings;
import com.city.parking.provider.ParkingProviderHelper;
import com.city.parking.provider.ParkingProviderHelper.Car;
import com.city.parking.provider.ParkingProviderHelper.Parking;
import com.city.parking.provider.ParkingRecord;
import com.city.parking.util.SharePrefUtil;
import com.city.parking.util.Utils;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.CursorAdapter;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MyParkingsActivity extends FragmentActivity implements OnClickListener {
    private ParkingListAdapter mAdapter;
    private Cursor mCursor = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_parkings);
		TextView titleTv = (TextView) findViewById(R.id.nav_title);
		titleTv.setText("我收藏的停车场");

        ImageView back = (ImageView) findViewById(R.id.nav_ico_back);
        back.setOnClickListener(this);

        ImageView more = (ImageView) findViewById(R.id.nav_ico_more);
        more.setVisibility(View.GONE);
        
        mCursor = ParkingProviderHelper.getAllParkings(this);
        if (mCursor == null || mCursor.getCount() == 0) {
            Toast.makeText(this, "您还未收藏任何停车场", Toast.LENGTH_SHORT).show();
        }

        ListView listView = (ListView) findViewById(R.id.my_parkings_list);
        mAdapter = new ParkingListAdapter(this, mCursor);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int pos,
                    long arg3) {
            	ParkingRecord r = mAdapter.getParking(pos);
            	if (r != null) {
					Intent intent = getIntent();
					ParkingItem item = new ParkingItem(r);
					intent.putExtra("item", item);
					setResult(RESULT_OK, intent);
            	} else {
					setResult(RESULT_CANCELED, null);
            	}
            	finish();
            }

        });
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.nav_ico_back:
            finish();
            break;
        case R.id.nav_ico_more:
            break;

        default:
            break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private class ViewHolder {
        TextView parkingName;
        TextView parkingAddr;

        double longitude;
        double latitude;
    }

    private class ParkingListAdapter extends CursorAdapter {
        private LayoutInflater inflater = null;

        public ParkingListAdapter(Context context, Cursor cursor) {
            super(context, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            inflater = LayoutInflater.from(context);
        }
        
        public ParkingRecord  getParking(int pos) {
            Cursor cursor = getCursor();
            if (cursor == null || !cursor.moveToPosition(pos)) {
               return null;
            }
            
            return ParkingProviderHelper.fromCursor(cursor);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Cursor cursor = getCursor();
            if (cursor == null || !cursor.moveToPosition(position)) {
                throw new IllegalStateException(
                        "couldn't move cursor to position "
                                + position);
            }

            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.parking_item, null);
				holder.parkingName = (TextView) convertView.findViewById(R.id.parking_name);
				holder.parkingAddr = (TextView) convertView.findViewById(R.id.parking_addr);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            
            String name = cursor.getString(cursor.getColumnIndex(Parking.COLUMN_NAME));
            String addr = cursor.getString(cursor.getColumnIndex(Parking.COLUMN_ADDR));
            double lng = cursor.getDouble(cursor.getColumnIndex(Parking.COLUMN_LONGITUDE));
            double lat = cursor.getDouble(cursor.getColumnIndex(Parking.COLUMN_LATITUDE));

            holder.parkingAddr.setText(addr);
            holder.parkingName.setText(name);
            holder.latitude = lng;
            holder.latitude = lat;

            return convertView;
        }

        @Override
        public void bindView(View arg0, Context arg1, Cursor arg2) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            return null;
        }
    }
}
